package test.jvm;

/**
 * 线程太多也会占满栈区域，报出异常：Exception in thread "main" java.lang.OutOfMemoryError:unable to create new native thread
 * 不过在windows上运行这个例子要小心，会出现系统假死的情况，有可能需要重启机器才行(实际情况会把操作系统的内存全部占满)！！！
 *
 */
public class StackOOM2 {

	/**
	 * @param args
	 */
	
	private int stackLength = 1;
	
	private void dontStop(){
		while(true){
			try{Thread.sleep(1000);}catch(Exception err){}
		}
	}
	
	public void stackLeakByThread(){
		while(true){
			Thread t = new Thread(new Runnable(){

				@Override
				public void run() {
					// TODO Auto-generated method stub
					dontStop();
				}
				
			});
			t.start();
			stackLength++;
			System.out.println(stackLength);
		}
	}
	
	public static void main(String[] args) throws Throwable{
		// TODO Auto-generated method stub
		StackOOM2 oom = new StackOOM2();
		try{
			oom.stackLeakByThread();
		}catch(Throwable err){
			System.out.println("Stack length:" + oom.stackLength);
			throw err;
		}
		
	}

}
